package com.atguigu.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.atguigu.constant.RedisConstant;
import com.atguigu.dao.SetmealDao;
import com.atguigu.pojo.Setmeal;
import com.atguigu.service.SetmealService;
import com.atguigu.vo.PageResult;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import redis.clients.jedis.JedisPool;

import java.util.List;
import java.util.Map;

@Service(interfaceClass = SetmealService.class)
@Transactional
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    SetmealDao setmealDao;

    @Autowired
    JedisPool jedisPool;

    @Override
    public PageResult fainPage(String queryString, Integer currentPage, Integer pageSize) {
        PageHelper.startPage(currentPage,pageSize);
        Page page = setmealDao.findPage(queryString);
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public void add(Integer[] travelgroupIds, Setmeal setmeal) {
        setmealDao.add(setmeal);
        Integer setmealId =setmeal.getId();
        setSetmealAndTravelGroup(travelgroupIds,setmealId);
        String img = setmeal.getImg();
        jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_DB_RESOURCES,img);
    }

    @Override
    public Setmeal get(Integer id) {
        Setmeal setmeal = setmealDao.get(id);
        jedisPool.getResource().srem(RedisConstant.SETMEAL_PIC_DB_RESOURCES,setmeal.getImg());
        return setmeal;
    }

    @Override
    public Integer[] getSetmealIdByTravelgroupId(Integer id) {
        return setmealDao.getSetmealIdByTravelgroupId(id);
    }

    @Override
    public void deleteSetmeal(Integer id) throws RuntimeException {
        Integer count = setmealDao.selectSetmealIdByTravelgroupId(id);
        if (count != null && count > 0){
            throw new RuntimeException("有关联");
        }

        Setmeal setmeal = setmealDao.get(id);
        String img = setmeal.getImg();
        setmealDao.deleteSetmeal(id);

        jedisPool.getResource().srem(RedisConstant.SETMEAL_PIC_DB_RESOURCES,img);
    }

    @Override
    public List<Setmeal> findAll() {
        return setmealDao.findAll();
    }

    @Override
    public Setmeal findById(Integer id) {
        return setmealDao.findById(id);
    }

    @Override
    public Setmeal getSetmeal(Integer id) {
        return setmealDao.getSetmeal(id);
    }

    @Override
    public List<Map<String, Object>> getSetmealReport() {
        return setmealDao.getSetmealReport();
    }

    @Override
    public void update(Integer[] travelgroupIds, Setmeal setmeal) {
        Integer setmealId = setmeal.getId();
        setmealDao.delete(setmealId);

        setmealDao.update(setmeal);

        setSetmealAndTravelGroup(travelgroupIds,setmealId);
        String img = setmeal.getImg();
        jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_DB_RESOURCES,img);
    }

    private void setSetmealAndTravelGroup(Integer[] travelgroupIds,Integer setmealId){
        if (travelgroupIds != null && travelgroupIds.length>0){
            for (Integer travelgroupId : travelgroupIds) {
                setmealDao.setSetmealAndTravelGroup(setmealId,travelgroupId);
            }
        }
    }
}
